<template>
  <div class="loading-wrapper">
    <div class="login-container" >
      <el-form :model="user" :rules="rules2"
               status-icon
               ref="ruleForm2"
               label-position="left"
               label-width="0px"
               class="demo-ruleForm login-page">
        <p class="title">登录</p>
        <el-form-item prop="name">
          <el-input type="text"
                    v-model="user.name"
                    auto-complete="off"
                    placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    v-model="user.password"
                    auto-complete="off"
                    placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-checkbox v-model="checked" class="rememberme">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登录</el-button>
          <p class="register" @click="gotoRegister">没有账号？立即注册</p>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script>
  import axios from 'axios'

  export default {
    data(){
      return {
        logining: false,
        user: {
          name:'',
          password:'',
        },
        rules2: {
          name: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'}]
        },
        checked: false
      }
    },
    methods: {
      gotoRegister() {
        this.$router.push({
          path: "/register"
        });
      },
      handleSubmit(event){
        this.$refs.ruleForm2.validate((valid) => {
          if(valid){
            axios.post("/api/user/login",this.user).then(res=>{
              if (res.data.code==200){
                alert(res.data.mgs);
                sessionStorage.setItem('user', this.user.name);
                this.$router.push({path: '/index'});

              } else{
                alert(res.data.mgs);
              }
            })
            // this.logining = true;
            // if(this.login()){
            //   this.logining = false;
            //   //sessionStorage.setItem('username', this.user.name);
            //   this.$router.push({path: '/'});
            // }else{
            //   this.logining = false;
            //   this.$alert('用户名或密码错误!', 'info', {
            //     confirmButtonText: 'ok'
            //   })
            // }
          }else{
            console.log('提交失败!');
            return false;
          }
        })
      }
    }
  };
</script>

<style>
  .loading-wrapper {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    background: #aedff8;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .register {
    margin-top: 10px;
    font-size: 14px;
    line-height: 22px;
    color: #1ab2ff;
    cursor: pointer;
    text-align: left;
    text-indent: 8px;
    width: 160px;
  }
  .login-container {
    width: 100%;
    height: 100%;
  }
  .title {
    font-size: 26px;
    line-height: 50px;
    font-weight: bold;
    margin: 10px;
    text-align: center;
  }
  .login-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  label.el-checkbox.rememberme {
    margin: 0px 0px 15px;
    text-align: left;
  }
</style>
